﻿using System;
using System.Collections;
using System.Collections.Generic;
using SKS;
using UnityEngine;
using UnityEngine.UI;

public class ApplicationToken1 : MonoBehaviour,IApplicationToken
{
    public int tokenClass = 1;
    private TouchEngine tm;
    private Vector2 tokenPosition;
    public Text pointShow;
    
    void Awake()
    {
        tm = TouchEngine.Instance;
    }
    
    void OnDestroy()
    {
        tm.TokenPlacedOnScreen -= OnTokenPlacedOnScreen;
        tm.TokenRemovedFromScreen -= OnTokenRemovedFromScreen;
    }

    private void OnEnable()
    {
        tm.ScreenTokenUpdated += OnTokenUpdated;
    }
    
    private void OnDisable()
    {
        tm.ScreenTokenUpdated -= OnTokenUpdated;
    }
    
    private void Start()
    {
        tm.TokenPlacedOnScreen += OnTokenPlacedOnScreen;
        tm.TokenRemovedFromScreen += OnTokenRemovedFromScreen;
        gameObject.SetActive(false);
    }

    public int TokenClass
    {
        get => tokenClass;
        set => throw new NotImplementedException();
        
    }

    public void OnTokenPlacedOnScreen(object sender, ApplicationTokenEventArgs e)
    {
        //Debug.Log("Toke1 显示");
        tokenPosition=new Vector2(e.Token.Position.x, e.Token.Position.y);
        transform.position = tokenPosition;
        transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, e.Token.Angle);
        gameObject.SetActive(true);
        pointShow.text=String.Format($"{e.Token.Points[0].Index}:{e.Token.Points[1].Index}:{e.Token.Points[2].Index}");
    }

    public void OnTokenRemovedFromScreen(object sender, ApplicationTokenEventArgs e)
    {
        gameObject.SetActive(false);
    }

    public void OnTokenUpdated(object sender, ApplicationTokenEventArgs e)
    {
        tokenPosition=new Vector2(e.Token.RuntimePosition.x, e.Token.RuntimePosition.y);
        transform.position = tokenPosition;
        transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, e.Token.Angle);
        pointShow.text=String.Format($"{e.Token.Points[0].Index}:{e.Token.Points[1].Index}:{e.Token.Points[2].Index}");
    }

}
